#include "BlinkingText.h"
#include "..\Managers\RenderManager.h"
#include "..\Engines\RenderEngine.h"

BlinkingText::BlinkingText(unsigned int p_view, const std::string& p_string, const std::string& p_font, float p_layer) : m_blinkState(0), m_fadeStrength(0.0f), m_active(true)
{
	m_text = RenderManager::getSingleton().getRenderEngine()->createText(p_view, p_font, p_string, p_layer);
	m_text->setOrigin(m_text->getLocalBounds().w * 0.5f, m_text->getLocalBounds().h * 0.5f);
	m_text->setColor(vn::Color(m_text->getColor().r, m_text->getColor().g, m_text->getColor().b, 0));
}


BlinkingText::~BlinkingText(void)
{
	RenderManager::getSingleton().getRenderEngine()->destroyText(m_text);
}

void BlinkingText::update(const GameTime& p_gameTime)
{
	if (m_active)
	{
		float alpha = static_cast<float>(m_text->getColor().a);
		if (m_blinkState == 0)
		{
			alpha = std::max(alpha - (m_fadeStrength * p_gameTime.deltaTime()), 0.0f);
			if (alpha == 0.0f)
				m_blinkState = 1;
		}
		else if (m_blinkState == 1)
		{
			alpha = std::min(alpha + (m_fadeStrength * p_gameTime.deltaTime()), 255.0f);
			if (alpha == 255.0f)
				m_blinkState = 0;
		}
		m_text->setColor(vn::Color(m_text->getColor().r, m_text->getColor().g, m_text->getColor().b, static_cast<unsigned char>(alpha)));
	}
}

void BlinkingText::setDoRender(bool p_value)
{
	m_text->setDoRender(p_value);
}

void BlinkingText::setActive(bool p_value)
{
	m_active = p_value;
	m_text->setDoRender(p_value);
}